package kr.hkit.androidcomplete03.ch25;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class WordDbHelper extends SQLiteOpenHelper {
	
	private static final String DB_NAME = "EngWord.db";			// DB 이름
	private static final int DB_VERSION = 3;					// DB 버전

	public WordDbHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		Log.d("TAG", "WordDbHelper()");
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.d("TAG", "onCreate()");
		String sql = "create table dic (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
				"eng TEXT, han TEXT);"; 
		db.execSQL(Sql.CREATE_TBL);
		for(int i=0; i<Sql.INSERT_TBL.length; i++){
			db.execSQL(Sql.INSERT_TBL[i]);
		}
		
		/*db.execSQL("insert into dic values(null, 'boy', '소년');");
		db.execSQL("insert into dic values(null, 'girl', '소녀');");
		db.execSQL("insert into dic values(null, 'hand', '손');");
		db.execSQL("insert into dic values(null, 'foot', '발');");
		db.execSQL("insert into dic values(null, 'head', '머리');");*/

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.d("TAG", "onUpgrade()" + "oldVersion : "+ oldVersion + "newVersion : " + newVersion);
		// db.execSQL("drop table if exists dic");
		db.execSQL(Sql.DROP_TBL);
		onCreate(db);
	}
	
	/*
	 * Sql클래스 내에 DB이름, 생성, 삽입, 삭제 구문선언
	 */
	static class Sql{
		public static final String DB_NAME = "dic";
		public static final String CREATE_TBL = "create table dic (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
				"eng TEXT, han TEXT);";
		
		public static final String DROP_TBL = "drop table if exists dic";
		public static final String[] INSERT_TBL = {
			"insert into "+DB_NAME+" values(null, 'boy', '소년');",
			"insert into "+DB_NAME+" values(null, 'girl', '소녀');",
			"insert into "+DB_NAME+" values(null, 'hand', '손');",
			"insert into "+DB_NAME+" values(null, 'foot', '발');",
			"insert into "+DB_NAME+" values(null, 'head', '머리');",
			};
	}

}
